package com.hzaupj.sb;

import com.hzaupj.sb.vo.Food;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by pengjian_91 on 2015/3/10.
 */
public class FoodParse {

    public List<Food> getFoodList(String hallId, String url) throws IOException {
        List<Food> foods = new ArrayList<Food>();
        Document doc = Jsoup.connect(url).timeout(5000).get();
        Elements eles = doc.select("div[class~=(pic-food|text-food )]");
        for(int i=0; i<eles.size(); i++){
            String picUrl = getPicUrl(eles.get(i));
            String[] foodInfos = getFoodInfo(eles.get(i));
            Food food = new Food();
            food.setHallId(hallId);
            food.setName(foodInfos[0]);
            double price = Double.valueOf(foodInfos[1]).doubleValue()*10.0;
            food.setPrice(Double.valueOf(price).intValue());
            food.setId(foodInfos[2]);
            food.setPicUrl(picUrl);
            if(Integer.valueOf(foodInfos[3])>0){
                food.setOnSale(1);
                foods.add(food);
            }
        }
        return foods;
    }

    private String getPicUrl(Element element) throws IOException {
        Elements eles = element.select("div.avatar");
        String patternStr = "data-src=\"(\\S*)\"";
        for(Element el : eles){
            String els = el.toString();
            Pattern pattern = Pattern.compile(patternStr);
            Matcher m = pattern.matcher(els);
            if(m.find()){
               return m.group(1);
            }
        }
        return null;
    }

    private String[] getFoodInfo(Element element){
        String[] results = new String[4];

        Elements eles = element.select("script").select("[type=text/template]").select("[id~=foodcontext*]");
        for(Element el : eles){
            String temp = el.toString()
                    .replaceAll(" ", "").replaceAll("\"", "")
                    .replaceAll("\\n", "").replaceAll("\\r", "");
            Pattern pattern = Pattern.compile("name:(\\S*?),");
            Matcher m = pattern.matcher(temp);
            if(m.find()){
                results[0] = m.group(1);
            }

            pattern = Pattern.compile("price:([,.\\d]*)");
            m = pattern.matcher(temp);
            if(m.find()){
                results[1] = m.group(1).replaceAll(",","");
            }

            pattern = Pattern.compile("id:(\\S*?),");
            m = pattern.matcher(temp);
            if(m.find()){
                results[2] = m.group(1);
            }
            pattern = Pattern.compile("onSale:(\\S*?),");
            m = pattern.matcher(temp);
            if(m.find()){
                results[3] = m.group(1);
            }else{
                results[3] = "0";
            }
        }
        return results;
    }

    public static void main(String[] strings){
        Pattern pattern = Pattern.compile("price:([,.\\d]*)");
        Matcher m = pattern.matcher("price:1,200,id:123");
        if(m.find()){
            System.out.print("result =" + m.group(1));
        }
    }
}
